#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
vector<int> vi, wi, si;
int n{0}, v{0}, tmp1{0}, tmp2{0}, tmp3{0}, a[5000][5000];  // 数组开大一点



int main()
{
    
    cin >> n >> v;
    int tmp = 0;
    for(int i = 0; i < n; i++)
    {
        cin >> tmp1 >> tmp2 >> tmp3;
        if(tmp3 > 0)
        {
            tmp += tmp3-1;   // 将多重背包拆成多个01背包
            for(int i = 0; i < tmp3; i++)
            {
                vi.push_back(tmp1);
                wi.push_back(tmp2);
                si.push_back(-1);
            }
        }
        else{
        vi.push_back(tmp1);
        wi.push_back(tmp2);
        si.push_back(tmp3);
        }
        
    }
    n += tmp;
    for(int i = 0; i < vi[0]; i++) a[i][0] = 0;
    if(si[0] == -1 )  // 准备
    {
        for(int i = vi[0]; i <= v; i++) a[i][0] = wi[0]; 
    } 
    else if(si[0] == 0)
    {
        for(int i = vi[0]; i <= v; i++)
        {
            int k = i / vi[0];
            a[i][0] = k*wi[0];
        }
    }

    // easy to learn by graph 0v0

    for(int j = 1; j < n; j++)  // the j th object
    {
        for(int i = 0; i <= v; i++) // volumn of the bag
        {
            if(i-vi[j] < 0)  // can't throw in the bag
            {
                a[i][j] = a[i][j-1];
            }
            else  // can throw in
            {
                if(si[j] == -1 || si[j] == 1)
                {
                    a[i][j] = max(a[i][j-1], a[i-vi[j]][j-1]+wi[j]);
                }  
                else if(si[j] == 0)
                {
                    int value1 = max(a[i][j-1], a[i-vi[j]][j-1]+wi[j]);
                    a[i][j] = max(value1, a[i-vi[j]][j]+wi[j]);
                }
            }   
        }
    }
    // for(int i = 0; i <= 4; i++) cout << a[i][0] << " ";
    
        // for(int j = 0; j < n; j++)
        // {
        //     for(int i = 0; i <= v; i++)
        //     {
        //         cout << a[i][j] << " ";
        //     }
        //     cout << endl;
        // }
            
    cout << a[v][n-1] << endl;
    // cout << 1 << endl;
    return 0;
}

